import { Sound } from '@babylonjs/core';
import { RawAssetsUrl } from '../../raw-assets-info';
import { DefaultStage } from '../default-stage';


class SoundManager {

    public static background: Sound;
    public static noteStart: Sound;
    public static countdown: Sound;
    public static over: Sound;
    public static explode: Sound;
    public static fire: Sound;
    public static shell: Sound;

    public static readonly shellSoundThreshold = 800 * 800;
    public static readonly shellSoundOffset = 800 / 340;

    public static initialize(stage: DefaultStage): void {
        const loader = stage.loader(stage.mainScene);

        const ratio = 1;
        const soundMusic = loader.getAudioTaskInfo(RawAssetsUrl.soundMusic)!.sound!;
        soundMusic.setVolume(0.3 * ratio);
        soundMusic.loop = true;

        const soundNoteStart = loader.getAudioTaskInfo(RawAssetsUrl.soundNoteStart)!.sound!;
        soundNoteStart.setVolume(0.8 * ratio);

        const explode = loader.getAudioTaskInfo(RawAssetsUrl.soundExplode)!.sound!;
        explode.setVolume(1.2 * ratio);

        const over = loader.getAudioTaskInfo(RawAssetsUrl.soundOver)!.sound!;
        over.setVolume(0.8 * ratio);

        const countdown = loader.getAudioTaskInfo(RawAssetsUrl.soundCountdown)!.sound!;
        countdown.setVolume(0.8 * ratio);

        const fire = loader.getAudioTaskInfo(RawAssetsUrl.soundFire)!.sound!;
        fire.setVolume(0.4 * ratio);

        const shell = loader.getAudioTaskInfo(RawAssetsUrl.soundShell)!.sound!;
        shell.setVolume(0.2 * ratio);

        this.background = soundMusic;
        this.noteStart = soundNoteStart;
        this.over = over;
        this.explode = explode;
        this.countdown = countdown;
        this.fire = fire;
        this.shell = shell;

        // stage.onUserGestureObservable.add((debut) => {
        //     if (!debut) return;
        //     Data.userGesture.value = true;
        // });
    }

    public static reset(): void {
        this.background.stop();
        this.noteStart.stop();
        this.over.stop();
        this.countdown.stop();
        this.explode.stop();
        this.fire.stop();
        this.shell.stop();
    }

}


export { SoundManager };